package com.sgb.kjwl.view.widget;

import android.content.Context;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.widget.LinearLayout;
import android.widget.TextView;

import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.user.TalentTypeEntity;
import com.sgb.kjwl.view.ui.adapter.self.publish.SkillAdapter;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.util.MToast;

import java.util.ArrayList;
import java.util.List;

public class SkillPopWindow extends BasePopWindow {

    private RecyclerView recyclerView;
    private TextView okView;
    private TextView resetView;
    private LinearLayout bottomLayout;
    private SkillAdapter skillAdapter;
    private List<TalentTypeEntity> talentTypeChildList;
    private List<TalentTypeEntity> selectList;
    private boolean isChange;
    private List<String> skillNo;

    public SkillPopWindow(Context context) {
        super(context, DIR_DOWN_UP);
    }

    @Override
    protected int popLayout() {
        return R.layout.popwindow_skill;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        recyclerView = holder.getPop().findViewById(R.id.skill_list);
        bottomLayout = holder.getPop().findViewById(R.id.bottom_layout);
        okView = holder.getPop().findViewById(R.id.ok_view);
        resetView = holder.getPop().findViewById(R.id.reset_view);
        selectList = new ArrayList<>();
        okView.setOnClickListener(v -> {
            dissMiss();
            if (onResultClick != null) {
                if (selectList == null) {
                    MToast.showToast(mContext, "请选择");
                } else {
                    onResultClick.result(selectList, null,null);
                }

            }
        });

        resetView.setOnClickListener(v -> dismiss());

        skillAdapter = new SkillAdapter(mContext, null);
        skillAdapter.setMultiSelect(true);
        skillAdapter.setDefItem(-1);

        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(skillAdapter);
        skillAdapter.setOnItemMultiClick((talentTypeEntities, position) ->
                selectList = talentTypeEntities);
    }

    public void setData(List<TalentTypeEntity> talentTypeChildList, boolean isChange, List<String> skillNo) {
        this.talentTypeChildList = talentTypeChildList;
        this.isChange = isChange;
        this.skillNo = skillNo;
        if (isChange) {
            skillAdapter.resetCheckDatas();
        }
        skillAdapter.setDatas(this.talentTypeChildList);
        if (skillNo != null) {
            for (int i = 0; i < skillNo.size(); i++) {
                for (int j = 0; j < talentTypeChildList.size(); j++) {
                    if (skillNo.get(i).equals(talentTypeChildList.get(j).getNo())) {
                        skillAdapter.checkDatas(j);
                    }
                }
            }
        }
    }

    /*public void setAlreadyData(List<String> skillNo) {
        this.skillNo = skillNo;
        skillAdapter.notifyDataSetChanged();
    }*/
}
